package com.application.Activity;

import java.util.ArrayList;

import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.application.Adapter.ListPartAdapter;
import com.application.Adapter.ListPartAdapter.ViewPart;
import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.DTO.PartDTO;
import com.application.DataProvider.DetailLearningDAO;
import com.application.DataProvider.PartDAO;

public class FrmPart extends Activity implements OnItemClickListener{
	private static ListView _listPart;	
	private static ArrayList<ViewPart> _arrayWork = new ArrayList<ListPartAdapter.ViewPart>();
	private static ListPartAdapter _arrayAdapter;
	private static ArrayList<PartDTO> _partList = new ArrayList<PartDTO>();
	private static int _idChap;
	private static Context _cont = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_part);

		NetworkController.init_runnableUpdatePart();
		_listPart = (ListView)findViewById(R.id.frmpart_lvListPart) ;
		_cont = this;
		Log.d("PEERDROID","Da send toi FrmPart");
		//ShowPart();

		//		TextView tvUserName = (TextView)findViewById(R.id.part_tvUsername);
		//		tvUserName.setOnClickListener(new OnClickListener() {

		//			@Override
		//			public void onClick(View arg0) {
		//				// TODO Auto-generated method stub
		//				Intent iIntent = new Intent(FrmPart.this,FrmConversation.class);
		//				startActivity(iIntent);
		//			}
		//		});	
		//		tvUserName.setText(NetworkController.get_userName());
		ShowPart();
		Log.d("PEERDROID","truoc set oncli");
		//Su kien click item on listview
		_listPart.setOnItemClickListener( this);
		Log.d("PEERDROID","thoat create");
	}

	public static void updateListAdapter()
	{
		Log.d("PEERDROID","Da vao update ShowPart");
		_partList.clear();
		Log.d("PEERDROID","_arrayWork: " + _arrayWork.toString());
		if(!_arrayWork.isEmpty())
			_arrayWork.clear();
		DetailLearningDAO dlDao = new DetailLearningDAO(null);
		PartDAO pDAO = new PartDAO(null);
		_partList= pDAO.getPartList(get_idChap());
		try {
			int n = _partList.size();
			Log.d("PEERDROID","n: " + n);
			for(int i=0;i<n;i++)
			{
				ViewPart view;
				String sName = _partList.get(i).get_Name();
				String sNote = _partList.get(i).get_Note();
				int idPart = _partList.get(i).get_ID();

				boolean status = pDAO.getStatus(idPart); //part new
				int idDetailLearning = dlDao.getIDDetailLearning(idPart, NetworkController.get_idClass());
				int idFeelType = dlDao.getIDFeelType(idDetailLearning); //co feeltype hay ko
				boolean isfeeltype = true;
				if(idFeelType == 0)
					isfeeltype = false;
				String path = ReadWriteFileController.DIRECTORY+idPart+"_"+NetworkController.get_userName()+"_FullTest_"+idDetailLearning+".xml";
				int sttFeedback ;
				if(dlDao.getStatus(idDetailLearning))
					sttFeedback = 1;
				else 
					sttFeedback = 0;
				//				if(ReadWriteFileController.isFile(path))
				//				{
				//					
				//					Log.d("PEERDROID","sttFeedback:" + sttFeedback);
				//					int sttFullTest = ReadXML.getIntValue(path, "Status");
				//					view=new ViewPart(sName,sNote,idPart, status, true, isfeeltype);
				//					view.setSttFulltest(sttFullTest);
				//					view.setSttFeedback(sttFeedback);
				//					view.setIdDetailLearning(idDetailLearning);
				//				}
				//				else 
				//				{
				//					view=new ViewPart(sName,sNote,idPart, status, false, isfeeltype);
				//					view.setSttFeedback(sttFeedback);
				//				}
				//view.setSttFulltest(sttFullTest);
				//_arrayWork.add(view);
			}

			setListAdapter();
		} catch (android.database.SQLException e) {
			Log.d("PEERDROID", "Query error!");
		}
	}
	public static void setListAdapter() {
		Log.d("PEERDROID","set list adapter");
		_arrayAdapter=new ListPartAdapter(_cont,R.layout.listrow_listpart,_arrayWork);
		_listPart.setAdapter(_arrayAdapter);

	}

	//Show danh sach bai hoc cua 1 mon hoc cu the
	private void ShowPart(){
		Log.d("PEERDROID","Da vao ShowPart");
		_partList.clear();
		if(!_arrayWork.isEmpty())
			_arrayWork.clear();
		Bundle bBundle = this.getIntent().getExtras();	
		DetailLearningDAO dlDao = new DetailLearningDAO(this);
		PartDAO pDAO = new PartDAO(this);
		set_idChap(Integer.valueOf(bBundle.getString("id")));
		_partList= pDAO.getPartList(get_idChap());
		try {
			int n = _partList.size();
			Log.d("PEERDROID","n: " + n);
			for(int i=0;i<n;i++)
			{
				ViewPart view;
				//lấy các thông tin cần thiết cho item part
				String sName = _partList.get(i).get_Name();
				String sNote = _partList.get(i).get_Note();
				int idPart = _partList.get(i).get_ID();

				//kiểm tra part có phải là part mới hay không?
				boolean status = pDAO.getStatus(idPart); //part new

				//kiểm tra nếu có 1 trong các thông báo: có bài tập, có feedback, có kết quả bài tập thì hiển
				//thị thông báo ra màn hình
				boolean isNotice = false;
				//kiểm tra có bài tập mới hay không?
				int idDetailLearning = dlDao.getIDDetailLearning(idPart, NetworkController.get_idClass());
				String path = ReadWriteFileController.DIRECTORYTESTCONTENT+idPart+"_"+NetworkController.get_userName()+"_FullTest_"+idDetailLearning+".xml";
				if(ReadWriteFileController.isFile(path) && ReadXML.getIntValue(path, "Status") == 0)
				{
					isNotice = true;
				}
				//kiểm tra có active feedback hay không?
				if(dlDao.getStatus(idDetailLearning))
				{

				}
				//kiểm tra có kết quả bài tập hay không?
				if(ReadWriteFileController.isFile(path) && ReadXML.getIntValue(path, "Status") == 2)
				{
					isNotice = true;
				}
				if(ReadWriteFileController.isFile(path))
				{

					view=new ViewPart(sName,sNote,idPart, status, true);
					view.setIdDetailLearning(idDetailLearning);
				}
				else 
				{
					view=new ViewPart(sName,sNote,idPart, status, false);
				}
				//view.setSttFulltest(sttFullTest);
				_arrayWork.add(view);
			}

			setListAdapter();
		} catch (android.database.SQLException e) {
			Log.d("PEERDROID", "Query error!");
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			ActivityController.handler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					FrmChapter.updateListAdapter();
				}
			});
		}

		return super.onKeyDown(keyCode, event);	
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
	//	ActivityController.setPeerActivity(this);
	}
	//--- Xu ly su kien ItemClick tren listview
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		// TODO Auto-generated method stub
		//Log.d("PEERDROID","Da vao ham Onclick");
		showFrmDetailPart(pos);		
	}
	private void showFrmDetailPart(int iPos) {

		Log.d("PEERDROID","Chuan bi Show part");

		String idPart = String.valueOf(_partList.get(iPos).get_ID());
		PartDAO pDao = new PartDAO(null);
		pDao.updatePart(false, Integer.valueOf(idPart));
		//Send id
		Intent iIntent = new Intent(FrmPart.this,FrmDetailPart.class);
		Bundle bBun = new Bundle();
		bBun.putString("id", idPart);
		Log.d("PEERDROID","id part frm part:" + idPart);
		iIntent.putExtras(bBun);
		StackActivity activityStack = (StackActivity) getParent();
		activityStack.push("FrmDetailPart", iIntent);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart part");
	//	ActivityController.setPeerActivity(this);
	}

	@Override
	public void onPause(){
		super.onPause();
	//	ActivityController.setPeerActivity(this);
	}
	public static void set_idChap(int _idChap) {
		FrmPart._idChap = _idChap;
	}

	public static int get_idChap() {
		return _idChap;
	}

}

